from atlassian import Confluence
from datetime import datetime

confluence = Confluence(
    url='http://doc.wangwuli.com:99/',
    username='admin',
    password='admin')


space = "SPC"
status = confluence.get_all_spaces()

now = datetime.now()
# 提取年、月、日
current_year = now.strftime('%Y')
current_month = now.strftime('%Y%m')
current_day = now.strftime('%Y%m%d')

# test = confluence.get_page_id("SPC", current_year)

# test2 = confluence.get_page_space(test)
# 创建基于日期的目录结构
if not confluence.page_exists(space, current_year):
    confluence.create_page(space, current_year, current_year, parent_id=None, type='page', representation='storage', editor='v2', full_width=False)
if not confluence.page_exists(space, current_month):
    current_year_id = confluence.get_page_by_title(space, current_year)
    confluence.create_page(space, current_month, current_month, parent_id=current_year_id, type='page', representation='storage', editor='v2', full_width=False)
if not confluence.page_exists(space, current_day):
    current_month_id = confluence.get_page_by_title(space, current_month)
    confluence.create_page(space, current_day, current_day, parent_id=current_month_id, type='page', representation='storage', editor='v2', full_width=False)

current_day_id = confluence.get_page_by_title(space, current_day)

# 定义页面内容
page_title = 'New Page Title2123'
page_content = '''
<h2>Heading</h2>
<p>This is a new page created using Python Confluence API.</p>
'''

# 创建页面
if not confluence.page_exists(space, page_title):
    page_id_obj = confluence.create_page(space=space, title=page_title, body=page_content, parent_id=current_day_id)
    now_page_id = page_id_obj['id']
    # 上传文件
    file_path = 'C:\\Users\\wangk\\Desktop\\1_1111111111111_jno2.tif'
    confluence.attach_file(file_path, page_id=now_page_id)
    # 设置页面标签
    # confluence.set_page_label(page_id, label)
else:
    now_page_id = confluence.get_page_by_title(space, page_title)

print(111)